//
//  JWFMDBHostpotTool.m
//  见闻新闻
//
//  Created by brother on 16/3/18.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWFMDBHostpotTool.h"
#define ID @"a_id"
#define name @"name"
#define title @"title"
#define appkey @"appkey"
#define html @"html"
#define pic  @"pic"
#define a_ID @"rd_id"


@implementation JWFMDBHostpotTool

static FMDatabase *_db;
+ (NSString *)getFilePath {
    NSLog(@"%@", [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"Hostpotdb.sqlite3"]);
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"Hostpotdb.sqlite3"];
}

+ (FMDatabase *)shareDataBase {
    if (_db == nil) {
        _db = [FMDatabase databaseWithPath:[self getFilePath]];
        if (![_db open]) {
            return nil;
        }else {
            NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS t_hostpot(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, html TEXT, appkey TEXT,pic TEXT, title TEXT, rd_id TEXT)"];
            BOOL sussecc = [_db executeUpdate:sql];
            if (sussecc) {
                NSLog(@"创建成功");
            }else {
                NSLog(@"创建失败");
            }
        }
    }
    return _db;
}
#pragma mark - 查询所有事件
+ (NSMutableArray *)selectAllNews {
    NSString *sql = [NSString stringWithFormat:@"select * from t_hostpot"];
    
    FMResultSet *resul = [[self shareDataBase] executeQuery:sql];
    
    NSMutableArray *mutArray = [NSMutableArray array];
    
    while ([resul next]) {
        JWFeatured *featured = [[JWFeatured alloc] init];
        featured.rd_id = [resul stringForColumn:@"jx_id"];
        featured.rd_name = [resul stringForColumn:@"name"];
        featured.rd_pic = [resul stringForColumn:@"pic"];
        featured.rd_title = [resul stringForColumn:@"title"];
        featured.rd_html = [resul stringForColumn:@"html"];
        featured.rd_appkey = [resul stringForColumn:@"appkey"];
        
        
        [mutArray addObject:featured];
    }
    
    
    return mutArray;
}
#pragma mark - 根据id查询
+ (NSMutableArray *)hostpotWithWhereId:(NSString *)rd_id {
    
    FMResultSet *result = [[self shareDataBase] executeQuery:@"SELECT * FROM t_hostpot WHERE rd_id = ?",rd_id];
    NSMutableArray *mutArray = [NSMutableArray array];
    while ([result next]) {
        JWFeatured *featured = [[JWFeatured alloc] init];
        featured.rd_id = [result stringForColumn:@"rd_id"];
        featured.rd_name = [result stringForColumn:@"name"];
        featured.rd_pic = [result stringForColumn:@"pic"];
        featured.rd_title = [result stringForColumn:@"title"];
        featured.rd_html = [result stringForColumn:@"html"];
        featured.rd_appkey = [result stringForColumn:@"appkey"];
        [mutArray addObject:featured];
    }
    
    return mutArray;
}
#pragma mark - 根据appkey类型查询
+ (NSMutableArray *)hostpotWithAppkey:(NSString *)rd_appkey {
    
    FMResultSet *result = [[self shareDataBase] executeQuery:@"SELECT * FROM t_hostpot WHERE appkey = ?",rd_appkey];
    NSMutableArray *mutArray = [NSMutableArray array];
    while ([result next]) {
        JWFeatured *featured = [[JWFeatured alloc] init];
        featured.rd_id = [result stringForColumn:@"rd_id"];
        featured.rd_name = [result stringForColumn:@"name"];
        featured.rd_pic = [result stringForColumn:@"pic"];
        featured.rd_title = [result stringForColumn:@"title"];
        featured.rd_html = [result stringForColumn:@"html"];
        featured.rd_appkey = [result stringForColumn:@"appkey"];
        [mutArray addObject:featured];
    }
    
    return mutArray;
}

+ (NSMutableArray *)searchNewsWithSearchText:(NSString*)searchText {
    return 0;
}
#pragma mark - 插入数据
+ (BOOL)insertNewsWithFeatured:(JWFeatured *)featured {
    BOOL sussecc = [[self shareDataBase] executeUpdate:@"INSERT INTO t_hostpot(name,pic,title,appkey,rd_id,html)values(?, ?, ?, ?, ?, ?)",featured.rd_name,featured.rd_pic,featured.rd_title,featured.rd_appkey,featured.rd_id,featured.rd_html];
    if (sussecc) {
        return YES;
    }else {
        return NO;
    }
}
#pragma mark - 修改数据
+ (BOOL)editNewsWithFeatured:(JWFeatured *)featured {
    NSString *sql = [NSString stringWithFormat:@"update t_hostpot set %@='%@',%@='%@',%@='%@',%@='%@',%@='%@' where rd_id = '%@'",name,featured.rd_name,pic,featured.rd_pic,title,featured.rd_title,appkey,featured.rd_appkey,html,featured.rd_html,featured.rd_id];
    BOOL sussecc =[[self shareDataBase] executeUpdate:sql];
    if (sussecc) {
        return YES;
    }else {
        return NO;
    }
}
#pragma mark - 删除数据
+ (BOOL)deleteNewsWithFeaturedId:(NSString *)rd_id {
    NSString *sql = [NSString stringWithFormat:@"delete from t_hostpot where %@ = '%@'",@"rd_id",rd_id];
    BOOL sussecc = [[self shareDataBase] executeUpdate:sql];
    if (sussecc) {
        return YES;
    }else {
        return NO;
    }
}

@end
